#include <windows.h>
#include "resource.h"
#include "func.h"

const char g_szClassName[] = "myWindowClass";
char x1[4],x2[4],x3[4],x4[4],x5[4],y1[4],y2[4],y3[4],y4[4],y5[4];
int SEM;
POINT poligon[5];
POINT Rect[4]={2000,2000,0,2000,0,0,2000,0};
POINT p1,p2,p3,p4,P1,P2;
POINT NUCLEU[10];
int IsPolygon(POINT);
int IsSimple(POINT);

int add_ok=0,n=0;
HWND nou=NULL;
HWND nou2=NULL;
BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_INITDIALOG:

        return TRUE;
        case WM_COMMAND:
			{
            switch(wParam)
            {
                case IDC_BUTTON1:
					{
                    GetDlgItemText(nou,IDC_EDIT1,y1,4);
					GetDlgItemText(nou,IDC_EDIT2,y2,4);
					GetDlgItemText(nou,IDC_EDIT3,y3,4);
					GetDlgItemText(nou,IDC_EDIT4,y4,4);
					GetDlgItemText(nou,IDC_EDIT5,y5,4);
					GetDlgItemText(nou,IDC_EDIT6,x1,4);
					GetDlgItemText(nou,IDC_EDIT7,x2,4);
					GetDlgItemText(nou,IDC_EDIT8,x3,4);
					GetDlgItemText(nou,IDC_EDIT9,x4,4);
					GetDlgItemText(nou,IDC_EDIT10,x5,4);
					poligon[0].x=atoi(x1);
					poligon[1].x=atoi(x2);
					poligon[2].x=atoi(x3);
					poligon[3].x=atoi(x4);
					poligon[4].x=atoi(x5);
					poligon[0].y=atoi(y1);
					poligon[1].y=atoi(y2);
					poligon[2].y=atoi(y3);
					poligon[3].y=atoi(y4);
					poligon[4].y=atoi(y5);
					
					EndDialog(hwnd, IDC_BUTTON1);
					if(poligon[0].x==0 || poligon[1].x==0 || poligon[2].x==0 || poligon[3].x==0 || poligon[4].x==0 ||
					   poligon[0].y==0 || poligon[1].y==0 || poligon[2].y==0 || poligon[3].y==0 || poligon[4].y==0)
					{
						
						MessageBox(hwnd,"Nu ati introdus toate punctele poligonului!", "Eroare!" ,MB_OK | MB_ICONERROR);
						add_ok=0;
						break;
					}
					else if(!IsPolygon(poligon))
					{
						MessageBox(hwnd,"Cel putin 3 puncte sunt colineare!", "Eroare!" ,MB_OK | MB_ICONERROR);
						add_ok=0;
						break;
					}
					/*else if(add_ok && !(IsSimple(poligon)))
					{
						MessageBox(hwnd,"Poligonul nu este simplu!", "Eroare!" ,MB_OK | MB_ICONERROR);
						add_ok=0;
						break;
					}*/
					else
						add_ok=1;

		for(int i=0;i<5;i++)
		{
			if(poligon[i].x<Rect[0].x)
				Rect[0].x=Rect[3].x=poligon[i].x;
			if(poligon[i].x>Rect[1].x)
				Rect[1].x=Rect[2].x=poligon[i].x;
			if(poligon[i].y<Rect[0].y)
				Rect[0].y=Rect[1].y=poligon[i].y;
			if(poligon[i].y>Rect[2].y)
				Rect[2].y=Rect[3].y=poligon[i].y;
		}
					
                break;
					}
            }
        break;
			}
		default:
            return FALSE;
    }
    return TRUE;
} // is the dialog procedure
//sln


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	HDC         hDC,hdc,ydc;
    PAINTSTRUCT Ps;
	HPEN	    hPen;
	HPEN		hPen2,hPen3;
	HBRUSH      brDiagCross  = CreateHatchBrush(HS_DIAGCROSS,  RGB(0, 128, 0));
    int ok=1;
	int i,j,ii,jj,x,y,xx,yy;
	
	switch(msg)
    {
		case WM_LBUTTONDOWN:
			char szFileName[MAX_PATH];
			MessageBox(hwnd,"Alegeti o optiune din meniu!", "Eroare!" ,MB_OKCANCEL | MB_ICONERROR);
			break;
			case WM_COMMAND:
				switch(LOWORD(wParam))
				{
				case ID_FILE_ADD:
							nou = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_FORMVIEW), hwnd , AboutDlgProc);
							if (nou)
								ShowWindow(nou,SW_NORMAL);
							else MessageBox(hwnd,"Eroare","Eroare",MB_OK);
						break;
					
				case ID_INFO_GEOMETRIECOMPUTATIONALA:
							MessageBox(hwnd,"Proiectul a fost realizat de Matei Cristian si Moldoveanu Dan.", "Despre..." ,MB_OK);
							break;
				case ID_FILE_EXIT:
							DestroyWindow(hwnd);
							break;
				case ID_AFISARE_NUCLEU:
					if(add_ok)
					{
					   SEM=1;
					   break;
					}
					else
					{
						MessageBox(hwnd,"Poligonul nu a fost introdus!", "Eroare!" ,MB_ICONERROR);
						SEM=0;
						break;
					}
				}
				//break;
		case WM_PAINT:
			{
				hdc = GetDC(hwnd);	
				
				hDC = BeginPaint(hwnd, &Ps);
				hPen = CreatePen(PS_DASH, 1, RGB(135, 100, 125));
				SelectObject(hDC, hPen);
				//******************
				
				hPen2 = CreatePen(PS_SOLID, 2, RGB(155, 5, 220));
				SelectObject(hDC, hPen2);
				if(add_ok)
				{
					p1.x=p4.x=Rect[0].x-10;p2.x=p3.x=Rect[2].x+10;
					p1.y=p2.y=Rect[0].y-10;p3.y=p4.y=Rect[2].y+10;
					MoveToEx(hdc, poligon[0].x,poligon[0].y, NULL);
					Polygon(hDC, poligon, 5);
				}
				if(SEM)
				{
					int pr=0;
					hPen2 = CreatePen(PS_DASH, 0, RGB(0, 0, 0));
					SelectObject(hdc, hPen2);
					poligon[5]=poligon[0];
					Rectangle(hdc,Rect[0].x-10,Rect[0].y-10,Rect[2].x+10,Rect[2].y+10);
					hPen2 = CreatePen(PS_SOLID, 2, RGB(155, 5, 220));
					SelectObject(hdc, hPen2);
					Polygon(hdc, poligon, 5);
					hPen2 = CreatePen(PS_DASH, 0, RGB(0, 0, 0));
					SelectObject(hdc, hPen2);
					NUCLEU[0].x=poligon[0].x;
					NUCLEU[0].y=poligon[0].y;
					for(i=0,j=i+1;i<5,j<6;i++,j++)
					{
						MoveToEx(hdc,poligon[j].x,poligon[j].y,NULL);
						//~~~Prelungire Linii~~~
						for(x=Rect[0].x-10;x<=Rect[2].x+10;x++)
							for(y=Rect[0].y-10;y<=Rect[2].y+10;y++)
							{
								P1.x=x;
								P1.y=y;
								if (colin(poligon[i],poligon[j],P1) &&
									( colin(P1,p1,p2) || colin(P1,p2,p3) || colin(P1,p3,p4) ||
									colin(P1,p1,p4) ))
								{
									LineTo(hdc,x,y);
									NUCLEU[++pr].x=x;
									NUCLEU[pr].y=y;
									break;
								}
							}
					}
						//~~~~~~~~~~~~~~~~~~~~~~
					//!?!?!?!?!?!?!?!?!?!?!?!?!?!?
					int color=0;
					NUCLEU[++pr]=NUCLEU[0];
					for(i=0,j=i+1;i<pr,j<pr+1;i++,j++)
					{
						hPen3 = CreatePen(PS_DOT, 0, RGB(color, color+10, color+20));
						color+=10;
						SelectObject(hdc, hPen3);
						//MoveToEx(hdc,poligon[j].x,poligon[j].y,NULL);
						//~~~Hasurare nucleu~~~
						for(x=Rect[0].x-10;x<=Rect[2].x+10;x++);
							for(y=Rect[0].y-10;y<=Rect[2].y+10;y++);
							{
								P1.x=x;
								P1.y=y;
								if (colin(NUCLEU[i],NUCLEU[j],P1))
								{
									//LineTo(hdc,x,y);
									MoveToEx(hdc,x-10,y-10,NULL);
									LineTo(hdc,x+10,y+10);
									break;
								}
							}
					}
					//!?!?!?!?!?!?!?!?!?!?!?!?!?!?
					///Creare si Desenare Nucleu///
					/*
					for(int x=0;x<10;x++)
						NUCLEU[x].x=NUCLEU[x].y=0;
					int t=-1,ii,jj;
					for(i=0,j=i+1;i<5,j<6;i++,j++)
					{
						//MoveToEx(hdc,poligon[j].x,poligon[j].y,NULL);
						//int w=0;
						for(x=Rect[0].x-10;x<=Rect[2].x+10;x++)
							for(y=Rect[0].y-10;y<=Rect[2].y+10;y++)
							{
								P1.x=x;
								P1.y=y;
								if (colin(poligon[i],poligon[j],P1))
									for(ii=0,jj=i+1;ii<5,jj<6;ii++,jj++)
									{
										if(ii==i){ ii++;jj++;}
										if(jj==j){ jj++;ii++;}
										for(x=min(poligon[ii].x,poligon[jj].x)-1;x<=max(poligon[ii].x,poligon[jj].x)+1;x++)
											for(y=min(poligon[ii].y,poligon[jj].y)-1;y<=max(poligon[ii].y,poligon[jj].y)+1;y++)
											{
												P2.x=x;P2.y=y;
												if(colin(poligon[ii],poligon[jj],P2) &&
													colin(poligon[i],P1,P2))
													//NUCLEU[++t]=P2;
													{MoveToEx(hdc,P2.x-5,P2.y-5,NULL);
												LineTo(hdc,P2.x+5,P2.y+5);}
											}
									}
							}
					}/*
					int B=0;
					for(B=0;NUCLEU[B].x!=0;B++)
					{}
					SelectObject(ydc, brDiagCross);
					Polygon(ydc,NUCLEU,B);
					*/
					/////////////////////////////////
				}//end of if (add_ok)
				EndPaint(hwnd, &Ps);
				break;
			}//end of case WM_PAINT
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU1);
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Inregistrarea ferestrei a esuat!", "Eroare!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "Geometrie Computationala",
        WS_OVERLAPPEDWINDOW,
        100, 15, 700, 600,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Crearea ferestrei a esuat!", "Eroare!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}